package com.zls.mapper;

import com.zls.pojo.Branch;
import com.zls.pojo.Project;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface BranchMapper {
    @Results(id = "BranchMap", value = {
            @Result(column = "bid", property = "bid"),
            @Result(column = "bname", property = "bname"),
            @Result(column = "updatetime", property = "updatetime"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "pid", property = "project", javaType = Project.class, one =
                    //FetchType.LAZY 懒加载   EAGER:立即加载
                    //select 代表的关联查询  调用指定的mapper方法
            @One(select = "com.com.zls.mapper.ProjectMapper.findByid", fetchType = FetchType.EAGER))
    })
    @Select("Select * from branch where status =1")
    List<Branch> findAll();

    @ResultMap("BranchMap")
    @Select("<script>" +
            "select * from branch where status =1" +
            "<if test='queryString!=null'> and sname like concat( '%',#{queryString},'%')" +
            "limit #{currentPage},#{pageSize}</if>" +
            "<if test='queryString==null'>limit #{currentPage},#{pageSize}</if> "+
            "</script>")
    List<Branch> findBra(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize, @Param("queryString") String queryString);

    //查询总记录数
    @Select("<script>" +
            "select count(*) from branch where status =1" +
            "<if test='queryString!=null'> and bname like concat( '%',#{queryString},'%') </if>" +
            "</script>")
    Long count(@Param("queryString") String queryString);

    @Insert("insert into branch (bid,bname,updatetime,pid)" +
            "values (#{bid},#{bname},#{updatetime},#{pid})")
    int addbranch(Branch branch);

    //根据bid查询
    @ResultMap("BranchMap")
    @Select("select * from branch where status =1 and bid=#{bid}")
    Branch findByBid(String bid);

    //修改
    @Update("update branch set bname=#{bname},pid=#{pid},updatetime=#{updatetime} where bid=#{bid} and status =1")
    int updatebranch(Branch branch);

    //逻辑删除
    @Update("update branch set status=0 where bid=#{bid}")
    int deletebranch(String bid);

    //根据pid查询
    @Select("select * from branch where status =1 and pid=#{pid}")
    List<Branch> findByPid(int pid);
}
